<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_addyourproperty extends CI_Model {
	function __construct() {
		parent::__construct();
	}

	function get_table() {
		$table = "Listings";

		return $table;
	}

	function getListingDescriptionTable() {
		$table = "ListingDescriptions";
		return $table;
	}

	function getListingMediaTable (){
		$table = "ListingMedia";
		return $table;
	}

	function getMemberTable() {
		$table = "Member";
		return $table;
	}

	//we also can do
	//var $table = "dfasfs";
	//and inside each function, we will change $table by $this->table

	function get($order_by) {
		$table = $this->get_table();

		$this->db->order_by($order_by);
		$query = $this->db->get($table);

		return $query;
	}

	function get_with_limit($limit, $offset, $order_by) {
		$table = $this->get_table();
		$this->db->limit($limit, $offset);
		$this->db->order_by($order_by);
		$query = $this->db->query($table);

		return $query;
	}

	function get_where($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$query = $this->db->get($table);

		return $query;
	}

	function get_where_custom($col, $value) {
		$table = $this->get_table();
		$this->db->where($col, $value);
		$query = $this->db->get($table);

		return $query;
	}

	function _insert($data) {
		$table = $this->get_table();
		$result = $this->db->insert($table, $data);

		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		return  $insert_id;
	}

	function _insertToListingDes($data) {
		$table = $this->getListingDescriptionTable();
		$result = $this->db->insert($table, $data);

		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		return  $insert_id;
	}

	function _insertToListingMedia($options) {
		$tsql_callSP = '{call spInsertListingMedia(?,?,?,?,?,?,?)}';

		$params = array(
			array($options['ListingFID'], SQLSRV_PARAM_IN),
			array($options['MediaType'], SQLSRV_PARAM_IN),
			array($options['FileName'], SQLSRV_PARAM_IN),
			array($options['FileExtension'], SQLSRV_PARAM_IN),
			array($options['FileData'], SQLSRV_PARAM_IN),
			array($options['IsActive'], SQLSRV_PARAM_IN),
			array($options['SortOrder'], SQLSRV_PARAM_IN)
		);

		$results = $this->db->exec_query($tsql_callSP, $params);

		if($results) {
			return true;
		}

		return false;
	}
	
	function addmediaphotos($options){
         $sql = "INSERT INTO dbo.ListingMedia (
			ListingFID, 
			MediaType, 
			FileName, 
			FileExtension, 
			FileData, 
			IsActive, 
			SortOrder
		)
        VALUES (
        ".$this->db->escape($options['ListingFID']).",
        ".$this->db->escape($options['MediaType']).",
        ".$this->db->escape($options['FileName']).",
        ".$this->db->escape($options['FileExtension']).",
        ".$this->db->escape($options['FileData']).",
        ".$this->db->escape($options['IsActive']).",
        ".$this->db->escape($options['SortOrder'])."
        )";
        
		
        $results = $this->db->query($sql);
        
        if($results) {
			return true;
		}

		return false;
	}

	function lastId($queryID) {
		sqlsrv_next_result($queryID);
		sqlsrv_fetch($queryID);
		return sqlsrv_get_field($queryID, 0);
	}

	function _insertToMember($data) {
		$table = $this->getMemberTable();
		$result = $this->db->insert($table, $data);

		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		return  $insert_id;
	}

	function _update($id, $data) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->update($table, $data);
	}

	function _delete($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->delete($table);
	}

	function count_where($column, $value) {
		$table = $this->get_table();
		$this->db->where($column, $value);
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();
		return $num_rows;
	}

	function count_all() {
		$table = $this->get_table();
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();

		return $num_rows;
	}

	function get_max() {
		$table = $this->get_table();
		$this->db->select_max('id');
		$query = $this->db->get($table);
		$row = $query->row();
		$id = $row->id;
		return $id;
	}

	function _custom_query($mysql_query) {
		$query = $this->db->query($mysql_query);
		return $query;
	}

	function getAgentInfo($pro_dir_id, $lang_id, $market) {
		$this->db->select('u.BusinessName,u.Mobile,u.Email');
		$this->db->from('dbo.Users u');
		$this->db->join('dbo.Listings l', 'u.User_ID = l.AgentFID');

		$where = array(
        	'l.Listing_ID'	=> $pro_dir_id,
        	'u.MarketFID'			=> $this->db->escape_str($market)
		);

		$this->db->where($where);

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}
	}

	function getTotalMemberByEmail($email) {
		$table = $this->get_table();
		 
		$this->db->select('*');
		$this->db->from($table);

		$where = array(
        	'Email'			=> $this->db->escape($email)
		);

		$this->db->where($where);

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}
}

?>
